package org.blog.conifg;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger API 配置类
 */
@EnableOpenApi
@Configuration
public class SwaggerConfig {
    //Docket分组，多个分组，即创建多个Docket实例
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .globalRequestParameters(this.getHeaderParameterList());
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("北辰轩雨","bcxy.com","2223677960@qq.com");
        return new ApiInfo(
                "多彩博客平台API文档",
                "愿年过半百，归来仍是少年",
                "1.0",
                "bcxy.com",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>()
        );
    }

    /**
     * 请求header 参数配置
     * @return
     */
    private List<RequestParameter> getHeaderParameterList() {
        RequestParameterBuilder requestParameterBuilder = new RequestParameterBuilder();
        List<RequestParameter> parameters = new ArrayList<>();
        requestParameterBuilder
                .name("Authorization")
                .description("Token令牌")
                .in(ParameterType.HEADER)
                .required(false) //非必填
                .build();
        parameters.add(requestParameterBuilder.build());
        return parameters;
    }
}